#! /usr/bin/env python
#
# Implement cookbook using pyfitsio
import sys
import os

import ctypes

# Use just the C-types bindings to CFITSIO
from fitsio import cfitsio, pyfitsio

from writeascii import put_table_data

def main():
    '''
    Create a binary table extension containing 3 columns and 6 rows
    '''

    # Put in some data
    planet = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn']
    diameter = [4880, 12112, 12742, 6800, 143000, 121000]
    density = [5.1, 5.3, 5.52, 3.94, 1.33, 0.69]

    filename = 'atestfil.fit'
    extname = 'PLANETS_Binary'
    nrows = 6

    # Column names, formats, and units. Note format for binary
    ttype = ['Planet', 'Diameter', 'Density']
    tform = ['8a', '1J', '1E']
    tunit = ['\0', 'km', 'g/cm']
    nrows = 6

    ff = pyfitsio.open(filename, mode='rw')

    # Move to 2nd HDU
    ff.move_to_hdu(2)

    # Append a new binary table
    ff.create_table(cfitsio.BINARY_TBL, extname, ttype, tform, tunit, nrows)

    put_table_data(ff, planet, diameter, density)

    ff.close()

if __name__ == '__main__':
    main()
